import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/contribution_entity.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/services.dart';
import 'default_bottom_sheet.dart';

/// SHG Contribution
showSHGContributionBottomSheet(
  BuildContext context, {
  double amount,
  ContributionEntity contribution,
}) async {
  double _amount = amount;
  ContributionEntity _contribution = contribution;
  await showDefaultBottomSheet(
    context,
    child: SHGContributionBottomSheet(
      amount: _amount,
      contribution: _contribution,
      selectContributionCallBack: (data) {
        _contribution = data;
      },
      selectAmountCallBack: (data) {
        _amount = data;
      },
    ),
    title: S.current.shg_contribution,
    isScrollControlled: true,
  );
  return {
    'amount': _amount,
    'contribution': _contribution,
  };
}

class SHGContributionBottomSheet extends StatefulWidget {
  final double amount;
  final ContributionEntity contribution;
  final ValueChanged<double> selectAmountCallBack;
  final ValueChanged<ContributionEntity> selectContributionCallBack;

  const SHGContributionBottomSheet(
      {Key key,
      this.amount,
      this.contribution,
      this.selectAmountCallBack,
      this.selectContributionCallBack})
      : super(key: key);

  @override
  _SHGContributionBottomSheetState createState() =>
      _SHGContributionBottomSheetState();
}

class _SHGContributionBottomSheetState
    extends State<SHGContributionBottomSheet> {
  List<String> filterContactsList2 = [
    'Statutory Default',
    'Custom Amount',
  ];

  bool get disableContribution => currentSelectSHG?.key == 'opted_out';

  ContributionEntity currentSelectSHG;
  TextEditingController amountController = TextEditingController();
  FocusNode amountFocusNode = FocusNode();
  double amount;

  String currentContributionAmount;

  bool get enable => currentSelectSHG != null;

  @override
  void initState() {
    super.initState();
    currentSelectSHG = widget.contribution;
    amount = widget.amount;
    if (amount != null && amount > 0) {
      currentContributionAmount = filterContactsList2.last;
    } else {
      currentContributionAmount = filterContactsList2.first;
    }
    if (amount != null && amount != 0.0) {
      amountController.text = amount?.toString();
    }
  }

  void _onApply() {
    amountFocusNode.unfocus();
    widget.selectAmountCallBack?.call(disableContribution == true
        ? null
        : (currentContributionAmount == filterContactsList2.first
            ? 0.0
            : double.parse(amountController.text, (e) => null)));
    widget.selectContributionCallBack?.call(currentSelectSHG);
    Navigator.of(context).pop();
  }

  void onChangeShg(ContributionEntity e) {
    amountFocusNode.unfocus();
    setState(() {
      currentSelectSHG = e;
      if (disableContribution) {
        currentContributionAmount = null;
        amount = null;
      } else {
        currentContributionAmount = filterContactsList2.first;
        amount = 0.0;
      }
    });
  }

  void onChangeContribution(String e) {
    amountFocusNode.unfocus();
    currentContributionAmount = e;
    setState(() {
      if (e == 'Statutory Default') {
        amount = 0;
      } else {
        amount = null;
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    amountController.dispose();
    amountFocusNode.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          height: getDp(17),
        ),
        _buildTop(S.current.shg),
        SizedBox(
          height: getDp(17),
        ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: getDp(30),
          ),
          child: Wrap(
            crossAxisAlignment: WrapCrossAlignment.center,
            alignment: WrapAlignment.start,
            children: Config.settingData.contribution
                .map((e) => PressedOpacityButton(
                      onPressed: () => onChangeShg(e),
                      padding: EdgeInsets.symmetric(
                        vertical: getDp(10),
                      ),
                      child: NormalRadio(
                        title: e.name,
                        minWidth: getDp(140),
                        isSelect: currentSelectSHG == e,
                      ),
                    ))
                .toList(),
          ),
        ),
        SizedBox(height: getDp(22)),
        _buildTop(S.current.contribution),
        SizedBox(
          height: getDp(17),
        ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: getDp(30),
          ),
          child: Wrap(
            crossAxisAlignment: WrapCrossAlignment.center,
            alignment: WrapAlignment.start,
            children: filterContactsList2
                .map((e) => PressedOpacityButton(
                      onPressed: !disableContribution
                          ? () => onChangeContribution(e)
                          : () {},
                      padding: EdgeInsets.symmetric(
                        vertical: getDp(10),
                      ),
                      child: NormalRadio(
                        title: LanguageUtils.translateFilter(e),
                        isFix: true,
                        isEnable: !disableContribution,
                        isSelect: e == filterContactsList2.first
                            ? amount == 0 && currentContributionAmount == e
                            : currentContributionAmount == e,
                      ),
                    ))
                .toList(),
          ),
        ),
        AppSpaces.h10,
        if (currentContributionAmount == filterContactsList2.last)
          InputField(
            tip: S.current.amount,
            controller: amountController,
            focusNode: amountFocusNode,
            keyboardType: TextInputType.numberWithOptions(decimal: true),
            inputFormatters: [doubleLimit2InputFormatter],
          ),
        SizedBox(
          height: getDp(30),
        ),
        NormalButton(
          onTap: enable ? _onApply : null,
          title: S.current.done,
        ),
      ],
    );
  }

  Widget _buildTop(String title) => Container(
        height: getDp(32),
        color: aceWhiteColor,
        padding: EdgeInsets.symmetric(horizontal: getDp(24)),
        alignment: Alignment.centerLeft,
        child: Text(
          title,
          style: TextStyle(
            color: aceDarkGrayColor,
            fontSize: getSp(16),
            fontWeight: FontWeight.w400,
            height: 22 / 16,
          ),
        ),
      );
}
